<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description" style="height: 420px">
        <WangEditor v-model="dataForm.description"></WangEditor>
      </el-form-item>
      <el-form-item label="数据集地址" prop="url">
<!--        <el-input v-model="dataForm.url" placeholder="数据集地址"></el-input>-->
        <el-button type="primary" @click="addFile(dataForm.id)">点击上传数据集文件</el-button>
      </el-form-item>
      <el-form-item label="开源许可证" prop="licenseId">
        <el-input v-model="dataForm.licenseName" placeholder="点击选择开源许可证" @click="addLicense"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
  <el-dialog v-model="licenseVisible" title="开源许可证" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <el-form-item>
        <el-input v-model="state.dataForm.name" placeholder="开源许可证名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="state.getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border highlight-current-row @current-change="handleCurrentChange" style="width: 100%">
      <el-table-column  header-align="center" align="center" width="50">
        <!--@change.native="handleSelectionChange(scope.$index,scope.row)"&ndash;&gt;type="selection"-->
        <template v-slot="scope">
          <el-radio v-model="dataForm.licenseId" :label="scope.row">{{""}}</el-radio>
        </template></el-table-column>
      <el-table-column prop="name" label="开源许可证名称" header-align="center" align="center"></el-table-column>
      <el-table-column prop="url" label="许可证地址" header-align="center" align="center"></el-table-column>
      <el-table-column prop="description" label="许可证描述" header-align="center" align="center"></el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit" :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle" @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="licenseSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
  <upload v-if="uploadVisible" ref="upload"  @fileList="getUrl"></upload>
</template>

<script lang="ts" setup>
import {nextTick, reactive, ref, toRefs} from "vue";
import baseService from "@/service/baseService";
import WangEditor from "@/components/wang-editor/index.vue";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
import useView from "@/hooks/useView";
import Upload from "@/views/poap/tfile-upload.vue";

const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const uploadVisible = ref(false)
const licenseVisible = ref(false)
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  name: "",
  description: "",
  url: "",
  licenseId: "",
  licenseName: "",
  delFlag: "",
  createDate: "",
  creator: "",
  updateDate: "",
  updater: "",
});

const view = reactive({
  getDataListURL: "/poap/poapopensourcelicense/page",
  getDataListIsPage: true,
  exportURL: "/poap/poapopensourcelicense/export",
  deleteURL: "/poap/poapopensourcelicense",
  deleteIsBatch: true,
  dataForm: {
    name: "",
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const license = ref([])

const rules = ref({
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/poap/poapdataset/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

const upload = ref()
const addFile = (id:string)=>{
  uploadVisible.value = true;
  nextTick(()=>{
    upload.value.init()
  })
}
/**
 *
 获取文件地址
 * @param e
 */
const getUrl = (e:any)=> {
  console.log(e);
  dataForm.url=e[0].response.data.url
  // dataForm.fileName = dataForm.fileName + " | " + dataForm.url[0].name;
}

const addLicense = ()=>{
  licenseVisible.value=true
}
const currentRow = ref()
const handleCurrentChange = (val:any) => {
  currentRow.value = val
}

const licenseSubmitHandle = ()=>{
  console.log(currentRow.value)
  dataForm.licenseId = currentRow.value.id
  dataForm.licenseName = currentRow.value.name
  licenseVisible.value=false
}

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/poap/poapdataset", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
